package com.hgd.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.annotations.JSON;

import com.hgd.client.chart.ChartImplDelegate;
import com.hgd.client.chart.ChartImplService;
import com.hgd.client.chart.TaskData;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ChartAction extends ActionSupport{
	private ChartImplService service = new ChartImplService();
	private ChartImplDelegate delegate = service.getChartImplPort();
	//private List<TaskData> dataList = new ArrayList<TaskData>();
	private TaskData taskData = new TaskData();
	private String dataParameters = null;
	//private StringBuffer dataParameters = null;
	private Integer taskId;
	private String taskKind;
	/**
	public String getCPUData() throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String timeLabels = null;
		String values = null;
		dataList = delegate.getTaskData(276);
		for (Iterator iterator = dataList.iterator(); iterator.hasNext();) {
			TaskData taskData = (TaskData) iterator.next();
			if (taskData.equals(dataList.get(0))) {
				timeLabels = sdf.format(XMLGCconvertToDate.convertToDate(taskData.getGainTime()));
				values = taskData.getDataValue();				
			}else{
				String timeLabel = sdf.format(XMLGCconvertToDate.convertToDate(taskData.getGainTime()));
				String value = taskData.getDataValue();
				timeLabels = timeLabels+","+timeLabel;
				values = values+","+value;
			}
			dataParameters = "&label=" +timeLabels+ "&value=" +values;
		}			
		//System.out.println(timeLabels);
		//System.out.println(values);
		return SUCCESS;
	}
	**/
	@JSON(serialize=false)
	public String getRealTimeAreaData() throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String timeLabels = null;
		String values = null;
		String returnStr = null;
		taskData = delegate.getSingelTaskData(taskId);
		if (taskData!=null){
			timeLabels = sdf.format(XMLGCconvertToDate.convertToDate(taskData.getGainTime()));
			values = taskData.getDataValue();
			dataParameters = "&label="+timeLabels+"&value="+values;	
			//dataParameters.append("&label="+timeLabels+"&value="+values).toString();
			returnStr = SUCCESS;
		} 
		return returnStr;
	}
	/**
	public String getMemoryData() throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String timeLabels = null;
		String values = null;
		String returnStr;
		taskData = delegate.getSingelTaskData(taskId);
		if (taskData!=null) {
			timeLabels = sdf.format(XMLGCconvertToDate.convertToDate(taskData.getGainTime()));
			values = taskData.getDataValue();
			dataParameters = "&label="+timeLabels+"&value="+values;	
			System.out.println(dataParameters);
			returnStr = SUCCESS;
		}else{
			returnStr = ERROR;
		}

		return returnStr;
	}
	**/
	@JSON(serialize=false)
	public String directChart() throws Exception{
		ActionContext actionContext = ActionContext.getContext();
        Map session = actionContext.getSession();
        //session.put("taskId", taskId);
        //session.put("taskKind", taskKind);      
		return SUCCESS;
	}
	
	
	@JSON(name="dataParameters") 
	public String getDataParameters() {
		return dataParameters;
	}
	public void setDataParameters(String dataParameters) {
		this.dataParameters = dataParameters;
	}
//	@JSON(name="dataParameters") 
//	public StringBuffer getDataParameters() {
//		return dataParameters;
//	}
//	public void setDataParameters(StringBuffer dataParameters) {
//		this.dataParameters = dataParameters;
//	}
	
	public Integer getTaskId() {
		return taskId;
	}
	public void setTaskId(Integer taskId) {
		this.taskId = taskId;
	}

	public String getTaskKind() {
		return taskKind;
	}

	public void setTaskKind(String taskKind) {
		this.taskKind = taskKind;
	}
}
